//
//  LXExtension+UIButton.swift
//  LXExtensions
//
//  Created by yoctech on 2021/8/31.
//

import Foundation
import UIKit

@objc public extension UIButton{
    
    @objc enum LXButtonImageMode: Int {
        case top, left, bottom, right
    }
    ///必须在layoutSubview后调用该方法
    @objc func lx_imageMode(_ mode : LXButtonImageMode, gap: CGFloat = 6.0) {
//        self.layoutIfNeeded()
        var imageWidth = self.imageView!.bounds.width
        var imageHeight = self.imageView!.bounds.height
        if imageWidth == 0 && imageHeight == 0 && self.imageView?.image != nil {
            imageWidth = self.imageView?.image?.size.width ?? 0
            imageHeight = self.imageView?.image?.size.height ?? 0
        }
        let labelHeight = self.titleLabel!.font.lineHeight
        
//        self.titleLabel?.sizeThatFits(CGSize(width: self.bounds.width, height: labelHeight))
        
        
        let labelWidth = self.titleLabel?.sizeThatFits(CGSize(width: CGFloat(MAXFLOAT), height: labelHeight)).width ?? 0 //labelWidth放第一行时获取出来为0
        // 这里都计算完毕了，很简单，例如 imageWidth是A  labelWidth是B
        // 那么image要居中就要X轴移动 （A+B）/2 - A/2
        // label要居中就要X轴移动 （A+B）/2 - B/2
        // Y轴移动就直接除以2 然后加上间隙就好了
        // 图片中心对齐控件XY轴的偏移量
        
        
        let imageOffSetX = labelWidth / 2;
        let imageOffSetY = imageHeight / 2;
        let labelOffSetX = imageWidth / 2;
        let labelOffSetY = labelHeight / 2;
        
        let maxWidth = max(imageWidth,labelWidth);
        // 上下排布宽度肯定变小 获取最大宽度的那个
        let maxHeight = max(imageHeight,labelHeight);
        // 获取最大高度那个 （就是水平默认排布的时候的原始高度）
        let changeWidth : CGFloat = imageWidth + labelWidth - maxWidth;
        // 横向缩小的总宽度
        let changeHeight : CGFloat = imageHeight + labelHeight - maxHeight + gap;
        // 总高度减去原始高度就是纵向宽大宗高度
        
        switch mode {
        case .top:
            self.imageEdgeInsets = UIEdgeInsets(top: -imageOffSetY - gap/2,
                                                left: imageOffSetX,
                                                bottom: imageOffSetY + gap/2,
                                                right: -imageOffSetX)
            self.titleEdgeInsets = UIEdgeInsets(top: labelOffSetY + gap/2,
                                                left: -labelOffSetX,
                                                bottom: -labelOffSetY - gap/2,
                                                right: labelOffSetX)
            self.contentEdgeInsets = UIEdgeInsets(top: changeHeight + CGFloat(gap) - labelOffSetY,
                                                  left: -changeWidth/2,
                                                  bottom: labelOffSetY,
                                                  right: -changeWidth/2)
        case .left:
            self.imageEdgeInsets = UIEdgeInsets(top: 0,
                                                left: CGFloat(-gap/2),
                                                bottom: 0,
                                                right: CGFloat(gap/2))
            self.titleEdgeInsets = UIEdgeInsets(top: 0,
                                                left: CGFloat(gap/2),
                                                bottom: 0,
                                                right: CGFloat(-gap/2))
            self.contentEdgeInsets = UIEdgeInsets(top: 0,
                                                  left: CGFloat(gap/2),
                                                  bottom: 0,
                                                  right: CGFloat(gap/2))
        case .bottom:
            self.imageEdgeInsets = UIEdgeInsets(top: imageOffSetY,
                                                left: imageOffSetX,
                                                bottom: -imageOffSetY,
                                                right: -imageOffSetX)
            self.titleEdgeInsets = UIEdgeInsets(top: -labelOffSetY,
                                                left: -labelOffSetX,
                                                bottom: labelOffSetY,
                                                right: labelOffSetX)
            self.contentEdgeInsets = UIEdgeInsets(top: labelOffSetY,
                                                  left: -changeWidth / 2,
                                                  bottom: changeHeight  - labelOffSetY,
                                                  right: -changeWidth / 2)
        case .right:
            self.imageEdgeInsets = UIEdgeInsets(top: 0,
                                                left: labelWidth + CGFloat(gap / 2) ,
                                                bottom: 0,
                                                right: -labelWidth - CGFloat(gap / 2))
            self.titleEdgeInsets = UIEdgeInsets(top: 0,
                                                left: -imageWidth - CGFloat(gap / 2),
                                                bottom: 0,
                                                right: imageWidth+CGFloat(gap / 2))
            self.contentEdgeInsets = UIEdgeInsets(top: 0,
                                                  left: CGFloat(gap / 2),
                                                  bottom: 0,
                                                  right: CGFloat(gap / 2));
        }
        
    }
    
    @objc convenience init(title:String? = nil, color:UIColor?, font: UIFont) {
        self.init()
        self.setTitle(title, for: .normal)
        self.setTitle(color: color, font: font)
    }
    
    @objc var title: String? {
        set {
            self.setTitle(newValue, for: .normal)
        }
        get {
            return self.title(for: .normal)
        }
    }
    
    @objc convenience init(imageName:String? = nil) {
        self.init()
        guard let imageName = imageName, !imageName.isEmpty else { return }
        self.setImage(UIImage(named: imageName), for: .normal)
    }
        
    @objc var imageName: String? {
        set {
            guard let imageName = newValue, !imageName.isEmpty else { return }
            self.image = UIImage(named: imageName)
        }
        get {
            return nil
        }
    }
    
    @objc var image: UIImage? {
        set {
            self.setImage(newValue, for: .normal)
        }
        get {
            return self.image(for: .normal)
        }
    }
    
    @objc func setTitle(color: UIColor?, font: UIFont) {
        self.setTitleColor(color, for: .normal)
        self.titleLabel?.font = font
    }
}
